#include <gtest/gtest.h>

#include <iostream>
#include "scoped_fd.hpp"
#include "shared_fd.hpp"
#include "weak_fd.hpp"

using namespace std;
void deleter(int fd) {
    cout << "delete " << fd << endl;
}

TEST(ScopedFd, base)
{
    cout << "=== begin ===" << endl;
    ScopedFd fd(1, deleter);

    cout << "aaaa:" << int(fd) << endl;
    fd.reset();
    if (true) {
        ScopedFd fd1(20);
        cout << "=== exit scope ==" << endl;
    }
    cout << "=== end ===" << endl;
}

void SharedFdTest(WeakFd w) {
    if (!w.expired()) {
        SharedFd s(w);
        printf("fd:%d\n", int(s));
    }
}

TEST(SharedFd, base)
{
    cout << "=== begin ===" << endl;
    SharedFd s1;
    if (true) {
        puts("==aa==");
        SharedFd s(11);
        s.reset();
        SharedFdTest(s);
        s1 = s;
        puts("==bb==");
    }
    SharedFdTest(s1);
    cout << "=== end ===" << endl;
}

TEST(SharedFd, vector) {
    SharedFd fd(33);
    std::vector<SharedFd> vec_fd;

    for (int i = 0; i < 100; ++i) {
        vec_fd.push_back(fd);
    }

    fd.reset();
}

TEST(SharedFd, compare) {
    SharedFd fd1(11);
    SharedFd fd2(fd1);
    SharedFd fd3(11);

    EXPECT_EQ(fd1, fd2);
    EXPECT_NE(fd2, fd3);
}
